package edu.gatech.gro.model.dao;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import edu.gatech.gro.model.User;

public class UserDao extends DatabaseHandler {

	private static final String TAG = "USER_DAO";

	public UserDao(Context context) {
		super(context);
	}

	@Override
	public void clear() {
		SQLiteDatabase db = this.getWritableDatabase();
		db.delete(USER_TABLE, "id > 0", null);
		db.close();
	}

	public ArrayList<User> getAllUsers() {
		ArrayList<User> list = new ArrayList<User>();
		String selectQuery = "SELECT  * FROM " + USER_TABLE;

		SQLiteDatabase db = this.getWritableDatabase();
		Cursor cursor = db.rawQuery(selectQuery, null);

		if (cursor.moveToFirst()) {
			do {
				User user = new User();
				user.setId(cursor.getInt(0));
				user.setUsername(cursor.getString(1));
				user.setUsernameClean(cursor.getString(2));
				user.setEmail(cursor.getString(3));
				user.setPassword(cursor.getString(4));
				user.setCreationTime(cursor.getInt(5));
				user.setLastUpdateTime(cursor.getInt(6));
				user.setDeleteFlag(cursor.getInt(7) == 1);
				list.add(user);
			} while (cursor.moveToNext());
		}
		db.close();

		return list;
	}

	public User getUserByUsernameAndPassword(String username, String password) {
		User user = null;
		SQLiteDatabase db = this.getReadableDatabase();

		Cursor cursor = db.rawQuery("SELECT  * FROM " + USER_TABLE + " WHERE username = ? AND password = ?", new String[] { username, password });
		if (cursor != null) {
			cursor.moveToFirst();

			user = new User();
			user.setId(cursor.getInt(0));
			user.setUsername(cursor.getString(1));
			user.setUsernameClean(cursor.getString(2));
			user.setEmail(cursor.getString(3));
			user.setPassword(cursor.getString(4));
			user.setCreationTime(cursor.getInt(5));
			user.setLastUpdateTime(cursor.getInt(6));
			user.setDeleteFlag(cursor.getInt(7) == 1);
		}
		cursor.close();
		db.close();

		return user;
	}

	public int count() {
		String countQuery = "SELECT  * FROM " + USER_TABLE;
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db.rawQuery(countQuery, null);
		cursor.close();
		return cursor.getCount();
	}

	public boolean saveUser(User user) {
		if (user == null) {
			return false;
		}

		SQLiteDatabase db = this.getWritableDatabase();

		ContentValues values = new ContentValues();
		values.put("id", user.getId());
		values.put("username", user.getUsername());
		values.put("usernameClean", user.getUsernameClean());
		values.put("email", user.getEmail());
		values.put("password", user.getPassword());
		values.put("creationTime", user.getCreationTime());
		values.put("lastUpdateTime", user.getLastUpdateTime());
		values.put("deleteFlag", user.isDeleteFlag() ? 1 : 0);

		long id = db.insert(USER_TABLE, null, values);
		if (id == -1) {
			// TODO: error
			return false;
		} else {
			user.setId((int) id);
		}

		db.close();
		return true;
	}

	public boolean updateUser(User user) {
		if (user == null || user.getId() < 1) {
			return false;
		}
		SQLiteDatabase db = this.getWritableDatabase();

		ContentValues values = new ContentValues();
		values.put("username", user.getUsername());
		values.put("usernameClean", user.getUsernameClean());
		values.put("email", user.getEmail());
		values.put("lastUpdateTime", user.getLastUpdateTime());
		values.put("deleteFlag", user.isDeleteFlag() ? 1 : 0);

		// updating row
		int r = db.update(USER_TABLE, values, "id = ?", new String[] { String.valueOf(user.getId()) });
		db.close();
		return r == 1;
	}

	public boolean deleteUser(User user) {
		if (user == null || user.getId() < 1) {
			return false;
		}
		SQLiteDatabase db = this.getWritableDatabase();
		int r = db.delete(USER_TABLE, "id = ?", new String[] { String.valueOf(user.getId()) });
		db.close();
		return r == 1;
	}
}
